package com.raycloud.erp.omll.provider.job.jobclient;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.spring.tasktracker.TaskId;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.raycloud.erp.omll.data.entity.MulPlatformUser;
import com.raycloud.erp.omll.data.entity.vo.OmallPullOrderVO;
import com.raycloud.erp.omll.data.mapper.OmallSyncInfoMapper;
import com.raycloud.erp.omll.provider.config.PullOrderConfig;
import com.raycloud.erp.omll.provider.model.bo.OrderIndexQueryBO;
import com.raycloud.erp.omll.provider.service.OrderIndexService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/***
 * @DESCRIPTION: 同步洋葱平台订单的LTS任务
 * @author 枫伦
 * @params:
 * @return:
 * @Date: 2021/1/12 10:00 下午
 * @Modified By:
 */
@LTS
@Component
@Slf4j
public class OmallPullOrderJob {
    @Autowired
    private OmallSyncInfoMapper omallSyncInfoMapper;
    @Autowired
    private OrderIndexService orderIndexService;
    @Autowired
    private PullOrderConfig pullOrderConfig;

    /***
     * @DESCRIPTION: 洋葱平台订单同步
     * @author 枫伦
     * @params: [jobContext]
     * @return: com.github.ltsopensource.tasktracker.Result
     * @Date: 2021/1/12 9:59 下午
     * @Modified By:
     */
    @TaskId("omall-pull-order-task")
    public Result jitPullOrderJob(JobContext jobContext) {
        try {
            //查询出所有用户的任务执行记录
            List<OmallPullOrderVO> omallPullOrderVOList = omallSyncInfoMapper.getOmallSyncInfoForTrade();
            if (CollectionUtil.isEmpty(omallPullOrderVOList)) {
                return new Result(Action.EXECUTE_SUCCESS,"此次任务没有用户的订单需要拉取");
            }
            OrderIndexQueryBO orderIndexQueryBO = new OrderIndexQueryBO();
            MulPlatformUser mulPlatformUser = new MulPlatformUser();

            for (OmallPullOrderVO omallPullOrderVO : omallPullOrderVOList) {
                //矫正配置的时间
                Date lastImportTradeTime = correctDate(omallPullOrderVO);
                //设置订单开始拉取时间
                orderIndexQueryBO.setStartTime(DateUtil.format(lastImportTradeTime, DatePattern.NORM_DATETIME_PATTERN));
                orderIndexQueryBO.setEndTime(DateUtil.now());
                orderIndexQueryBO.setPageNo(1);
                BeanUtils.copyProperties(omallPullOrderVO, mulPlatformUser);
                orderIndexService.saveOrderFromChannel(orderIndexQueryBO, mulPlatformUser);
            }
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了");
        } catch (Exception e) {
            log.error("同步Omall订单失败", e);
            return new Result(Action.EXECUTE_EXCEPTION, e.getMessage());
        }
    }

    private Date correctDate(OmallPullOrderVO omallPullOrderVO) {
        Date date = new Date();
        Date lastImportTradeTime = omallPullOrderVO.getLastImportTradeTime();
        long between = DateUtil.between(date, lastImportTradeTime, DateUnit.DAY);
        if (between > Integer.valueOf(pullOrderConfig.getFirstPullTimes())) {
            lastImportTradeTime = DateUtil.offsetDay(date, 0 - Integer.valueOf(pullOrderConfig.getFirstPullTimes()));
        }
        return lastImportTradeTime;
    }

    public static void main(String[] args) {
        String now = DateUtil.now();
        Date date = DateUtil.parse(now);
        System.out.println(date);


        System.out.println(DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        long between = DateUtil.between(new Date(), DateUtil.parse("2021-01-01 00:00:00", DatePattern.NORM_DATETIME_PATTERN), DateUnit.DAY);
        System.out.println(between);
    }
}
